local qianyi = fk.CreateSkill {
  name = "lvshi__qianyi",
}

Fk:loadTranslationTable{
  ["lvshi__qianyi"] = "骞逸",
  [":lvshi__qianyi"] = "你的非额外回合结束后，你可令此回合失去过牌和受到过伤害的其他角色依次视为对你使用【杀】，然后你进行一个只能对这些角色使用牌的额外回合。",
  ["#lvshi__qianyi"] = "骞逸：你可以进行一个额外的回合。",
  ["@[player]qianyi-turn"] = "骞逸",

  ["$lvshi__qianyi1"] = "吾等戮力同心，定能回天转日！",
  ["$lvshi__qianyi2"] = "孙家子弟，皆是善战之辈！",
}

qianyi:addEffect(fk.TurnEnd, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(qianyi.name) and target == player and not target:insideExtraTurn()
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {skill_name = qianyi.name, prompt = "#lvshi__qianyi"})
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local targets = {}
    room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function (e)
      for _, move in ipairs(e.data) do
        if move.from and move.from ~= player and not move.from.dead and
          (move.to ~= move.from or (move.toArea ~= Card.PlayerHand and move.toArea ~= Card.PlayerEquip)) then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
              table.insertIfNeed(targets, move.from)
            end
          end
        end
      end
    end, Player.HistoryTurn)
    player.room.logic:getActualDamageEvents(1, function(e)
      if player ~= e.data.to and not e.data.to.dead then
        table.insertIfNeed(targets, e.data.to)
      end
    end)
    if #targets > 0 then
      room:setPlayerMark(player, "qianyi-source", table.map(targets, Util.IdMapper))
      for _, p in ipairs(targets) do
        if p:isAlive() and player:isAlive() then
          room:useVirtualCard("lvshi__slash", nil, p, player, qianyi.name, true)
        end
      end
    end
    if not player.dead then
      player:gainAnExtraTurn(true, qianyi.name, nil, {
        qianyi_source = player,
      })
    end
  end,
})

qianyi:addEffect(fk.TurnStart, {
  is_delay_effect = true,
  mute = true,
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(qianyi.name) and data.reason == qianyi.name and data.extra_data
    and data.extra_data.qianyi_source == player
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local sources = player:getTableMark("qianyi-source")
    if #sources > 0 then
      room:setPlayerMark(player, "@[player]qianyi-turn", sources)
    end
    room:setPlayerMark(player, "qianyi-source", 0)
  end,
})

qianyi:addEffect("prohibit", {
  is_prohibited = function(self, from, to, card)
    if from:hasSkill(qianyi.name) and #from:getTableMark("@[player]qianyi-turn") ~= 0 then
      return not table.contains(from:getTableMark("@[player]qianyi-turn"), to.id)
    end
  end,
})


return qianyi